package com.xhs.main.controller.xsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.result.LayuiResponseResult;
import com.xhs.main.entity.xsb.Client;
import com.xhs.main.entity.xsb.Contract;
import com.xhs.main.entity.xsb.Order;
import com.xhs.main.service.xsb.impl.XsbOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hjr
 * @create 2020-10-20-下午 5:44
 */

@RestController
public class OrderController {

    @Autowired
    XsbOrderServiceImpl xsbOrderService;

    @RequestMapping("/addOrder")
    public  void  add(Order order){
        System.out.println(order);
        xsbOrderService.addOrder(order);
    }

    @RequestMapping("/getAllOrder")
    public LayuiResponseResult showAllContract(){
        List<Order> allOrder = xsbOrderService.getAllOrder();
        System.out.println(allOrder);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(allOrder.size());
        result.setData(allOrder);
        return  result;
    }

    @RequestMapping("/getOrd")
    public LayuiResponseResult getAllOrderBy(@RequestParam("searchParams") String searchParams){
        System.out.println(searchParams);
        Order order = JSONObject.parseObject(searchParams, Order.class);

        JSONObject jsonObject = JSONObject.parseObject(searchParams);
        String xsbCName = (String) jsonObject.get("xsbCName");
        Client client = new Client();
        client.setXsbCName(xsbCName);

        String xsbCoNo = (String) jsonObject.get("xsbCoNo");
        Contract contract = new Contract();
        contract.setXsbCoNo(xsbCoNo);

        order.setClient(client);
        order.setContract(contract);

        List<Order> allOrderByVague = xsbOrderService.getAllOrderByVague(order);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(allOrderByVague.size());
        result.setData(allOrderByVague);

        return result;
    }


    @RequestMapping("/updateOrder")
    public LayuiResponseResult update(@RequestParam("searchParams") String searchParams){

        System.out.println(searchParams);
        Order order = JSONObject.parseObject(searchParams, Order.class);


        List<Order> orders = xsbOrderService.updateOrder(order);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(orders.size());
        result.setData(orders);

        return result;
    }

}
